Ext.define('CA.dictionary.Window',{
    extend: 'Ext.Window',
    xtype: 'CA.dictionary.Window',

    uses: [
        'CA.dictionary.Tree',
        'CA.dictionary.Panel',
        'CA.dictionary.DataPanel'
    ],

    width: '90%',
    height: '90%',
    layout: 'border',
    border: false,

    initComponent: function(){
        Ext.apply(this, {
            items: [
                {
                    region: 'west',
                    xtype: 'CA.dictionary.Tree',
                    width: 300,
                    minWidth: 250,
                    split: true,
                    itemId: 'tree',
                    listeners: {
                        scope: this,
                        selectionchange: this.onSelectionChange
                    },
                    tbar: [
                        {
                            text: 'Обзор',
                            iconCls: 'c-icon-book',
                            disabled: true,
                            scope: this,
                            handler: this.dictionaryBrowse,
                            itemId: 'btnBrowse'
                        },
                        {
                            text: 'Редактировать',
                            iconCls: 'c-icon-book_edit',
                            scope: this,
                            handler: this.dictionaryEdit,
                            disabled: true,
                            itemId: 'btnEdit'
                        },
                        '->',
                        {
                            text: 'Удалить',
                            iconCls: 'c-icon-book_delete',
                            disabled: true,
                            scope: this,
                            handler: this.dictionaryRemove,
                            itemId: 'btnRemove'
                        }
                    ]
                },
                {
                    xtype: 'tabpanel',
                    itemId: 'tabs',
                    region: 'center',
                    resizeTabs: true,
                    enableTabScroll: true,
                    items: [{
                        title: 'Создать',
                        xtype: 'CA.dictionary.Panel',
                        iconCls: 'c-icon-book_add',
                        listeners: {
                            scope: this,
                            saveDictionary: this.onSaveDictionary
                        }
                    }]
                }
            ]
        });

        this.callParent();
    },

    onSaveDictionary: function(data){
        var tree = this.down('#tree');

        tree.createDictionary(data);
    },

    dictionaryBrowse: function(){
        var node = this.activeDictionary,
            tabs = this.down('#tabs'),
            tabId = 'browseTab_'+node.internalId,
            tab = tabs.down('#' + tabId),
            viewType = node.data.view_type;

        if(!tab){
            tab = tabs.add({
                xtype: 'CA.dictionary.DataPanel',
                layout: 'fit',
                title: node.data.name,
                iconCls: 'c-icon-book',
                itemId: tabId,
                dictionaryId: node.data.id,
                closable: true
            });
        }

        tabs.setActiveTab(tab);

    },

    dictionaryEdit: function(){
        var node = this.activeDictionary,
            tabs = this.down('#tabs'),
            tabId = 'editTab_'+node.internalId,
            browseTabId = '#browseTab_'+node.internalId,
            tab = tabs.down('#' + tabId);

        if(!tab){
            tab = tabs.add({
                xtype: 'CA.dictionary.Panel',
                title: node.data.name,
                iconCls: 'c-icon-book_edit',
                itemId: tabId,
                dictionaryId: node.data.id,
                editMode: true,
                closable: true,
                listeners: {
                    saveDictionary: function(data){
                        var browseTab = tabs.down(browseTabId);

                        if(browseTab) browseTab.setTitle(data.name);
                        node.set(data);
                    }
                }
            });
        }

        tabs.setActiveTab(tab);
    },

    dictionaryRemove: function(){
        var node = this.activeDictionary,
            data = node.raw ? node.raw : node.data,
            editTab = this.down('#editTab_'+node.internalId),
            browseTab = this.down('#browseTab_'+node.internalId),
            text = 'Вы действительно хотите удалить справочник "' + data.text + '"?';

        CA.checkRemove(text, function(){
            Dictionary.remove(data.id, data.code, function(resp){
                node.remove(true);
                this.checkActions(true);

                if(editTab) editTab.close();
                if(browseTab) browseTab.close();
            }, this);
        }, this);
    },

    onSelectionChange: function(sm, selected){
        var node = selected[0],
            type = node.raw ? node.raw.type : node.data.type,
            isGroup = type == 'group';

        if(!isGroup){
            this.activeDictionary = node;
        }

        this.checkActions(isGroup);
    },

    checkActions: function(disabled){
        var btnRemove = this.down('#btnRemove'),
            btnEdit = this.down('#btnEdit'),
            btnBrowse = this.down('#btnBrowse');

        btnRemove.setDisabled(disabled);
        btnEdit.setDisabled(disabled);
        btnBrowse.setDisabled(disabled);
    }
});